package com.prs.crm.action.online;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.online.ForumContent;
import com.prs.crm.domain.online.ForumContentReply;
import com.prs.crm.domain.online.ForumManager;
import com.prs.crm.service.online.ForumContentReplyService;
import com.prs.crm.service.online.ForumContentService;
import com.prs.crm.service.online.ForumManagerService;

public class ForumManagerAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private ForumManager forumManager;
	private ForumManagerService forumManagerService;
	private ForumContent forumContent;
	private ForumContentService forumContentService;
	
	private ForumContentReply forumContentReply;
	private ForumContentReplyService forumContentReplyService;
	
	private List<ForumContentReply> reply;
	private File upload;
	private String uploadFileName;
	private String title;
	private List<ForumManager> forum;
	public List<ForumManager> getForum() {
		return forum;
	}
	public void setForum(List<ForumManager> forum) {
		this.forum = forum;
	}
	public ForumContent getForumContent() {
		return forumContent;
	}
	public void setForumContent(ForumContent forumContent) {
		this.forumContent = forumContent;
	}
	public ForumContentService getForumContentService() {
		return forumContentService;
	}
	public void setForumContentService(ForumContentService forumContentService) {
		this.forumContentService = forumContentService;
	}
	public ForumContentReply getForumContentReply() {
		return forumContentReply;
	}
	public void setForumContentReply(ForumContentReply forumContentReply) {
		this.forumContentReply = forumContentReply;
	}
	public ForumContentReplyService getForumContentReplyService() {
		return forumContentReplyService;
	}
	public void setForumContentReplyService(
			ForumContentReplyService forumContentReplyService) {
		this.forumContentReplyService = forumContentReplyService;
	}
	public List<ForumContentReply> getReply() {
		return reply;
	}
	public void setReply(List<ForumContentReply> reply) {
		this.reply = reply;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public ForumManager getForumManager() {
		return forumManager;
	}
	public void setForumManager(ForumManager forumManager) {
		this.forumManager = forumManager;
	}
	public ForumManagerService getForumManagerService() {
		return forumManagerService;
	}
	public void setForumManagerService(ForumManagerService forumManagerService) {
		this.forumManagerService = forumManagerService;
	}
	@Action("listForumManager")
	public String listForumManager(){
		List<ForumManager> fr=this.getForumManagerService().getAllForumManager();
		for (ForumManager fm : fr) {
			String[] ma=fm.getForumManagerPerson().split(",");
			for (String ms : ma) {
				if (AppContext.getAccountContext().currentUser().getResume().getName().equals(ms)) {
					fm.setState(AppContext.getAccountContext().currentUser().getId()+":1");
					this.getForumManagerService().submit(fm);
					break;
				}else{
					fm.setState("2");
					this.getForumManagerService().submit(fm);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ForumManager.class);
		criteria.add(Restrictions.eq("state", AppContext.getAccountContext().currentUser().getId()+":1"));
		criteria.addOrder(Order.asc("sort"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getForumManagerService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listForumManagers")
	public String listForumManagers(){
		List<ForumManager> fr=this.getForumManagerService().getAllForumManager();
		for (ForumManager fm : fr) {
			/**可发帖人员*/
			String[] postPerson=fm.getPostedPerson().split(",");
			for (String po : postPerson) {
//				System.out.println(po);
				if (AppContext.getAccountContext().currentUser().getResume().getName().equals(po)) {
					fm.setTiezi("可发帖");
					this.getForumManagerService().submit(fm);
					break;
				}else{
					fm.setTiezi("不可发帖");
					this.getForumManagerService().submit(fm);
				}
			}
			/**可见人员*/
			String[] seePerson=fm.getSeePerson().split(",");
			for (String sperson : seePerson) {
//				System.out.println(sperson);
				if (AppContext.getAccountContext().currentUser().getResume().getName().equals(sperson)) {
					fm.setSeeState(AppContext.getAccountContext().currentUser().getId()+":1");
					this.getForumManagerService().submit(fm);
					break;
				}else{
					fm.setSeeState("false");
					this.getForumManagerService().submit(fm);
				}
			}
			/**版主*/
			String[] managerPerson=fm.getForumManagerPerson().split(",");
			for (String mperson : managerPerson) {
//				System.out.println(mperson);
				if (AppContext.getAccountContext().currentUser().getResume().getName().equals(mperson)) {
					fm.setTiezi("可发帖");
					fm.setIsDel("可删除");
					fm.setShowState(AppContext.getAccountContext().currentUser().getId()+":1");
					this.getForumManagerService().submit(fm);
					break;
				}else{
					fm.setShowState("false");
					fm.setIsDel("false");
					this.getForumManagerService().submit(fm);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ForumManager.class);
		criteria.add(Restrictions.or(Restrictions.eq("showState", AppContext.getAccountContext().currentUser().getId()+":1"), Restrictions.eq("seeState", AppContext.getAccountContext().currentUser().getId()+":1")));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getForumManagerService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editForumManager")
	public String editForumManager(){
		if(ids!=null){
			this.forumManager=this.getForumManagerService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveForumManager",results={
			@Result(name="success",type="redirect",location="listForumManager"),
			@Result(name ="error",type="chain",location="editForumManager")
	})
	public String saveForumManager(){
		if (forumManager.getName()==null || "".equals(forumManager.getName().trim())) {
			this.addActionError("论坛名称不能为空！");
			return ERROR;
		}
		if (forumManager.getForumManagerPerson()==null || "".equals(forumManager.getForumManagerPerson().trim()) || "0".equals(forumManager.getForumManagerPerson().trim())) {
			this.addActionError("论坛版主不能为空和长度不能为0！");
			return ERROR;
		}
		String[] len=forumManager.getForumManagerPerson().split(",");
		System.out.println(len.length);
		if(len.length>=6){
			this.addActionError("论坛版主最多只能设置5人！");
			return ERROR;
		}
		if (forumManager.getId()==null) {	
			List<ForumManager> fm=this.getForumManagerService().getAllForumManager();
			int f=0;
			for (ForumManager forum : fm) {
				if (forum.getName().equals(forumManager.getName())) {
					f++;
				}
			}
			if (f>0) {
				this.addActionError("该论坛名称已经存在，请重新输入！");
				return ERROR;
			}
			forumManager.setThemeNum(0);
			forumManager.setArticleNum(0);
			this.getForumManagerService().save(forumManager);
		}else{
			this.getForumManagerService().submit(forumManager);
		}
		return SUCCESS;
	}
	@Action(value="removeForumManager",results={
			@Result(name="success",type="redirect",location="listForumManager"),
			@Result(name ="error",type="chain",location="listForumManager")
	})
	public String removeForumManager(){
		if(ids!=null){
			this.forumManager=this.getForumManagerService().get(ids[0]);
		}
		if (forumManager.getForumContent().size()!=0) {
			this.addActionError("该论坛中已有帖子，不能删除！");
			return ERROR;
		}
		this.getForumManagerService().batchRemove(ids);
		return SUCCESS;
	}
}
